﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ZX.CRM.Library;
using System.Threading.Tasks;
using ZX.EHR.Library;
using System.Web.SessionState;
using ZX.BLL.Extend;
using ZX.Web.UI.Controls;
namespace ZX.WebApp.Crm.Hodometer
{
    /// <summary>
    /// NewInCalendar1 的摘要说明
    /// </summary>
    public class NewInCalendar1 : BasePage, IHttpHandler, IReadOnlySessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string hid = context.Request["HID"];
            string orderid = context.Request["orderid"];
            string sttime = context.Request["sttime"];
            string edtime = context.Request["edtime"];
            string tchid = context.Request["tchid"];
            string reason = context.Request["reason"];
            string color = context.Request["color"];
            context.Response.ContentType = "text/plain";
            //CodeSmithAutoCode csc = new CodeSmithAutoCode();
            int back = 1;
            //if (sttime == edtime)
            //{
            back = save(hid, orderid, Convert.ToDateTime(sttime), Convert.ToDateTime(edtime), tchid, reason, color);
            //}
            //else
            //{
            //    int d=(Convert.ToDateTime(edtime) - Convert.ToDateTime(sttime)).Days;
            //    if (d > 0)
            //    {
            //        Parallel.For(0, d+1, (i) => save(hid, orderid, Convert.ToDateTime(sttime).AddDays(i), tchid, reason, color));
            //    }
            //}
            context.Response.Write(back); 
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


        private int save(string id,string orderid,DateTime sdate, DateTime edate, string tchid,string reason,string color)
        {
            CrmInnerTeachHodometer cth = new CrmInnerTeachHodometer();
            cth.OrderId = new Guid(orderid); 
            cth.Addtime = DateTime.Now;
            cth.Reason = reason;
            cth.TeachID = new Guid(tchid);
            cth.BeginDate = sdate;
            cth.EndDate = edate;
            cth.backcolor = color;
            cth.AddUser = User.UserID;
            int back = 1;
            if (id == "")
            {
                try
                {
                    cth.ID = Guid.NewGuid();
                    BaseBussnies bbss = new BaseBussnies(cth);
                    if (orderid=="00000000-0000-0000-0000-000000000000")
                    {
                        bbss.delete("OrderId='00000000-0000-0000-0000-000000000000' and BeginDate between'" + cth.BeginDate + "' and '" + cth.EndDate + "'");
                    }
                    if (reason != "")
                    {
                        bbss.Insert();
                    }
                }
                catch(Exception)
                {
                    back = 0;
                }
            }
            else
            {
                try
                {
                    cth.ID = new Guid(id);
                    BaseBussnies bbss = new BaseBussnies(cth);
                    bbss.delete("OrderId='00000000-0000-0000-0000-000000000000' and BeginDate between'" + cth.BeginDate + "' and '" + cth.EndDate + "'");
                    bbss.Update();
                }
                catch (Exception)
                {
                    back = 0;
                }
                
            }

            return back;
        }
    }
}